<?php
/*


/-----------------------------------------------\
|          CODE by Kirati Isarapungpun          |
|       For North - Chiang Mai University       |
|       Software Engineering Project 2013       |
|                                               |
|  |-|>===================================<|-|  |
|                                               |
|       Course planning for Bachelor of         |
| Science degree in software engineering 2012   | 
|      Faculty of Science and Technology,       |
|        North - Chiang Mai University          |
|                                               |
\-----------------------------------------------/

/												\
			User	:	Class user    
\												/

*/
	class User
	{
		private $id;
		private $userId;
		private $userName;
		private $passWord;
		private $name;
		private $year;
		
		
		public function __construct()
		{
		}
		
		function getID()
		{
			return $this->id;
		}
		
		function setID($set)
		{
			$this->id = $set;
		}
		
		function getUserID()
		{
			return $this->userId;
		}
		
		function setUserID($set)
		{
			$this->userId = $set;
		}
		
		function getUserName()
		{
			return $this->userName;
		}
		
		function setUserName($set)
		{
			$this->userName = $set;
		}
		
		function getPassWord()
		{
			return $this->passWord;
		}
		
		function setPassWord($set)
		{
			$this->passWord = $set;
		}
		
		function getName()
		{
			return $this->name;
		}
		
		function setName($set)
		{
			$this->name = $set;
		}
		
		function getYear()
		{
			return $this->year;
		}
		
		function setYear($set)
		{
			$this->year = $set;
		}
		
		function setLogin($username,$password)
		{
			$this->userName = $username;
			$this->passWord = $password;
		}
		
		function setALL($id,$userId,$username,$password,$name,$year)
		{
			$this->id = $id;
			$this->userId = $userId;
			$this->userName = $username;
			$this->passWord = $password;
			$this->name = $name;
			$this->year = $year;
		}
		
		function findOne()
		{
			$query = mysql_query('SELECT U_name,U_user_id,U_username,U_password,U_year FROM user WHERE U_id = "'.$this->id.'" LIMIT 1')or die(mysql_error());
			$arr = mysql_fetch_object($query);
			$this->userId = $arr->U_user_id;
			$this->userName = $arr->U_username;
			$this->passWord = $arr->U_password;
			$this->name = $arr->U_name;
			$this->year = $arr->U_year;
			return($this->name);
		}
		
		function findOneJSON()
		{
			$query = mysql_query('SELECT U_name,U_user_id,U_username,U_password,U_year FROM user WHERE U_id = "'.$this->id.'" LIMIT 1')or die(mysql_error());
			$arr = mysql_fetch_object($query);
			$data_array[1]['name'] = $arr->U_name;
			$data_array[1]['userId'] = $arr->U_user_id;
			//$data_array[1]['username'] = $arr->U_username;
			//$data_array[1]['password'] = $arr->U_password;
			$data_array[1]['year'] = $arr->U_year;
			return json_encode($data_array);
		}
		
		function findAll()
		{
			$query = mysql_query('SELECT U_id,U_name,U_user_id,U_username,U_password,U_year FROM user')or die(mysql_error());
			$data_array = array();
			while($arr = mysql_fetch_object($query))
			{
				$data_array[$arr->D_id]['id'] = $arr->U_id;
				$data_array[$arr->D_id]['name'] = $arr->U_name;
				$data_array[$arr->D_id]['userId'] = $arr->U_user_id;
				$data_array[$arr->D_id]['username'] = $arr->U_username;
				$data_array[$arr->D_id]['password'] = $arr->U_password;
				$data_array[$arr->D_id]['year'] = $arr->U_year;
			}
			return json_encode($data_array);
		}
		
		public function login()
		{
			$query = mysql_query('SELECT U_id FROM user WHERE U_username = "'.$this->userName.'" AND U_password = "'.$this->passWord.'" LIMIT 1')or die(mysql_error());
			$count = mysql_num_rows($query) or die(mysql_error());
			if($count == 1)
			{
				$arr = mysql_fetch_object($query);
				$_SESSION['username'] = $arr->U_id;
				return $arr->U_id;
			} else
			{
				$status = user;
				return 0;
			}
		}
		
		function register()
		{
			$query = mysql_query('SELECT U_id FROM user WHERE U_username = "'.$this->userName.'" LIMIT 1')or die(mysql_error());
			$count = mysql_num_rows($query);
			if($count == 1)
			{
				return 0;
			} else
			{
				mysql_query('INSERT INTO user(U_user_id,U_username,U_password,U_name,U_year) VALUES("'.$this->userId.'","'.$this->userName.'","'.$this->passWord.'","'.$this->name.'","'.$this->year.'")')or die(mysql_error());
				return 1;
			}
		}
		
		public function sessionCheck()
		{
			if(isset($_SESSION['username'])) {
				return $_SESSION['username'];
			} else  {
				return 0;
			}
		}
		
		public function logout()
		{
			session_destroy();
		}
	}
?>